//
//  ViewController.m
//  MundyTest
//
//  Created by Tuan on 3/18/14.
//  Copyright (c) 2014 Tuan. All rights reserved.
//

#import "ViewController.h"
#import "Entity.h"

@interface ViewController ()
{
    
}

@end

@implementation ViewController
- (void)viewWillAppear:(BOOL)animated{
    self.invalidLbl.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
//    [self fetchRecords];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)checkCodeAction:(id)sender {
    NSLog(@"%@",self.codeTextField.text);
    if (self.codeTextField.text.length ==11) {
        [self checkCode:self.codeTextField.text];
        self.invalidLbl.hidden=YES;
    }else{
        self.invalidLbl.hidden=NO;
    }
}
- (void)checkCode:(NSString*)code{
    NSString * str1 = [code substringWithRange:NSMakeRange(0, 1)];
    NSString * str2 = [code substringWithRange:NSMakeRange(1, 1)];
    NSString * str3 = [code substringWithRange:NSMakeRange(2, 1)];
    NSString * str4 = [code substringWithRange:NSMakeRange(3, 1)];
    NSString * str5 = [code substringWithRange:NSMakeRange(4, 1)];
    NSString * str6 = [code substringWithRange:NSMakeRange(5, 1)];
    NSString * str7 = [code substringWithRange:NSMakeRange(6, 1)];
    NSString * str8 = [code substringWithRange:NSMakeRange(7, 1)];
    NSString * str9 = [code substringWithRange:NSMakeRange(8, 1)];
    NSString * str10 = [code substringWithRange:NSMakeRange(9, 1)];
    NSString * str11 = [code substringWithRange:NSMakeRange(10, 1)];
    
    //
    NSString * num2 = [self xNumber:str2];
    NSString * num4 = [self xNumber:str4];
    NSString * num6 = [self xNumber:str6];
    NSString * num8 = [self xNumber:str8];
    NSString * num10 = [self xNumber:str10];
    
    int total = [str1 intValue] + [str3 intValue] +[str5 intValue]+ [str7 intValue]+[str9 intValue]+[str11 intValue] + [num2 intValue] + [num4 intValue] + [num6 intValue] + [num8 intValue] +[num10 intValue];
    if ((total%10) == 0) {
        [self saveCoreDataWithNumb:code Result:@"Pass"];
    }else{
        [self saveCoreDataWithNumb:code Result:@"Fail"];
    }
    
}
- (NSString*)xNumber:(NSString*)number{
    NSString * numbX2 = [NSString stringWithFormat:@"%d",([number intValue]*2)];
    if (numbX2.length ==2) {
        NSString * subNumb1 = [numbX2 substringWithRange:NSMakeRange(0, 1)];
        NSString * subNumb2 = [numbX2 substringWithRange:NSMakeRange(1, 1)];
        
        NSString * totalNumb = [NSString stringWithFormat:@"%d",([subNumb1 intValue]+ [subNumb2 intValue])];
        
        return totalNumb;
    }
    return numbX2;
}
- (void)saveCoreDataWithNumb:(NSString*)numb Result:(NSString*)result{
    Entity * entity = (Entity *)[NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:_managedObjectContext];
    [entity setAccount:numb];
    [entity setResult:result];
    [entity setTime:[NSDate date]];
    NSError *error;
    
    if(![_managedObjectContext save:&error]){
        NSLog(@"%@",error);
    }
}

#pragma mark -
- (void)fetchRecords {
	_managedObjectContext =[NSManagedObjectContext new];
    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:_managedObjectContext];
    
    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];
    
    // Define how we will sort the records
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];
    
    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    
    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    }
    
    // Save our fetched data to an array

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dictData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    static NSDateFormatter *dateFormatter = nil;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"h:mm.ss a"];
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    
//    Event *event = [eventArray objectAtIndex: [indexPath row]];
//    Event *previousEvent = nil;
    
    return cell;

}
@end
